package org.example;


import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class PreparePanel extends JPanel {
    private SnakeGame main;
    private JComboBox<String> mapComboBox;
    private JComboBox<String> p1skillComboBox;
    private JComboBox<String> p2skillComboBox;
    private JComboBox<String> modeComboBox;
    private JComboBox<String> playerComboBox; // 新增玩家模式选择框
    private JComboBox<String> timeComboBox;
    // 添加玩家皮肤选择框
    private JComboBox<String> p1SkinComboBox;
    private JComboBox<String> p2SkinComboBox;

    public PreparePanel(SnakeGame main) {
        this.main = main;
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(800, 600));

        // 标题
        JLabel titleLabel = new JLabel("游戏准备", JLabel.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 36));
        titleLabel.setForeground(new Color(100, 200, 255));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(30, 0, 50, 0));
        add(titleLabel, BorderLayout.NORTH);

        // 设置面板
        JPanel settingsPanel = new JPanel();
        settingsPanel.setLayout(new GridLayout(4, 2, 20, 30));
        settingsPanel.setOpaque(false);
        settingsPanel.setBorder(BorderFactory.createEmptyBorder(20, 150, 50, 150));

        // 地图选择
        JLabel mapLabel = new JLabel("地图类型:", JLabel.RIGHT);
        mapLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        mapLabel.setForeground(Color.WHITE);
        settingsPanel.add(mapLabel);

        String[] mapTypes = {"无障碍", "有边界", "有固定障碍", "乱石","迷宫","落石","宝藏"};
        mapComboBox = new JComboBox<>(mapTypes);
        mapComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        settingsPanel.add(mapComboBox);

        // 模式选择
        JLabel modeLabel = new JLabel("游戏模式:", JLabel.RIGHT);
        modeLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        modeLabel.setForeground(Color.WHITE);
        settingsPanel.add(modeLabel);

        String[] modeTypes = {"经典模式", "加速模式", "饥饿模式"};
        modeComboBox = new JComboBox<>(modeTypes);
        modeComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        settingsPanel.add(modeComboBox);

        // 新增玩家模式选择
        JLabel playerLabel = new JLabel("玩家模式:", JLabel.RIGHT);
        playerLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        playerLabel.setForeground(Color.WHITE);
        settingsPanel.add(playerLabel);

        String[] playerModes = {"单人模式", "双人模式"};
        playerComboBox = new JComboBox<>(playerModes);
        playerComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        settingsPanel.add(playerComboBox);

        //新增时间选择
        JLabel timeLabel = new JLabel("时间限制:", JLabel.RIGHT);
        timeLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        timeLabel.setForeground(Color.WHITE);
        settingsPanel.add(timeLabel);

        String[] timeModes = {"无", "1分钟", "2分钟", "3分钟"};
        timeComboBox = new JComboBox<>(timeModes);
        timeComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        settingsPanel.add(timeComboBox);

        // 1技能选择
        JLabel p1skillLabel = new JLabel("p1技能选择:", JLabel.RIGHT);
        p1skillLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        p1skillLabel.setForeground(Color.WHITE);
        settingsPanel.add(p1skillLabel);

        String[] skillTypes = {"无", "双倍收获","冲刺","穿梭"};
        p1skillComboBox = new JComboBox<>(skillTypes);
        p1skillComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        settingsPanel.add(p1skillComboBox);

        // 2技能选择
        JLabel p2skillLabel = new JLabel("p2技能选择:", JLabel.RIGHT);
        p2skillLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        p2skillLabel.setForeground(Color.WHITE);
        settingsPanel.add(p2skillLabel);

        p2skillComboBox = new JComboBox<>(skillTypes);
        p2skillComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        settingsPanel.add(p2skillComboBox);

        //新增皮肤选择
        String[] skinModes = { "1.绿色", "2.蓝色", "3.紫色", "4.橙色", "5.霓虹", "6.粉色", "7.金色", "8.暗色"};
        JLabel p1skinLabel = new JLabel("p1皮肤选择:", JLabel.RIGHT);
        p1skinLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        p1skinLabel.setForeground(Color.WHITE);
        settingsPanel.add(p1skinLabel);

        p1SkinComboBox = new JComboBox<>(skinModes);
        p1SkinComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        settingsPanel.add(p1SkinComboBox);

        JLabel p2skinLabel = new JLabel("p2皮肤选择:", JLabel.RIGHT);
        p2skinLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        p2skinLabel.setForeground(Color.WHITE);
        settingsPanel.add(p2skinLabel);

        p2SkinComboBox = new JComboBox<>(skinModes);
        p2SkinComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        settingsPanel.add(p2SkinComboBox);

        add(settingsPanel, BorderLayout.CENTER);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 50, 30));
        buttonPanel.setOpaque(false);

        // 开始游戏按钮
        JButton startButton = createStyledButton("开始游戏", new Color(70, 180, 80), 180, 50);
        startButton.addActionListener(e -> {
            String mapType = (String) mapComboBox.getSelectedItem();
            String skillType1 = (String) p1skillComboBox.getSelectedItem();
            String skillType2 = (String) p2skillComboBox.getSelectedItem();
            String modeType = (String) modeComboBox.getSelectedItem();
            String playerMode = (String) playerComboBox.getSelectedItem();
            String timeMode = (String) timeComboBox.getSelectedItem();
            String skinType1 = (String) p1SkinComboBox.getSelectedItem();
            String skinType2 = (String) p2SkinComboBox.getSelectedItem();
            main.showGame(mapType, skillType1,skillType2, modeType, playerMode,timeMode,skinType1,skinType2);
        });
        buttonPanel.add(startButton);

        // 返回按钮
        JButton backButton = createStyledButton("返回菜单", new Color(200, 80, 80), 180, 50);
        backButton.addActionListener(e -> main.showMenu());
        buttonPanel.add(backButton);

        add(buttonPanel, BorderLayout.SOUTH);
    }

    private JButton createStyledButton(String text, Color color, int width, int height) {
        JButton button = new JButton(text);
        button.setFont(new Font("微软雅黑", Font.BOLD, 20));
        button.setBackground(color);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createRaisedBevelBorder());
        button.setPreferredSize(new Dimension(width, height));

        // 悬停效果
        button.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                button.setBackground(color.brighter());
            }
            public void mouseExited(MouseEvent e) {
                button.setBackground(color);
            }
        });

        return button;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 绘制背景
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 渐变背景
        GradientPaint gradient = new GradientPaint(0, 0, new Color(50, 50, 80), getWidth(), getHeight(), new Color(30, 30, 50));
        g2d.setPaint(gradient);
        g2d.fillRect(0, 0, getWidth(), getHeight());

        // 装饰性图案
        g2d.setColor(new Color(255, 255, 255, 30));
        for (int i = 0; i < 8; i++) {
            int size = 40 + i * 20;
            g2d.drawOval(getWidth()/2 - size/2, getHeight()/2 - size/2, size, size);
        }
    }
}